// JavaScript Document

var _vogue = {};
var util = {
    trim: function(str){
        return typeof str.trim === 'function' ? str.trim() : str.replace(/^\s+|\s+$/g, '');
    },
    contains: function(a, b){
        if (document.documentElement.contains) {
            this.contains = function(a, b){
                return a !== b && a.contains ? a.contains(b) : true;
            }
            
        }
        else 
            if (document.documentElement.compareDocumentPosition) {
                this.contains = function(a, b){
                    return !!(a.compareDocumentPosition(b) & 16);
                }
            }
        return this.contains(a, b);
    }
};



_vogue.slide = function(id_lbtn, id_rbtn, id_box, slideCount){
    var btnL = $(id_lbtn), btnR = $(id_rbtn), itembox = $(id_box), slideCount = typeof slideCount == 'number' && slideCount > 1 ? slideCount : 1;
    var items = itembox.children();
    var count = items.length;
    var itemW = items.eq(0).outerWidth();
    var marginR = parseInt(items.eq(0).css('marginRight'));
    var wholeWidth = (itemW + marginR) * count;
    var slideW = (itemW + marginR) * slideCount;
    itembox.css('width', wholeWidth);
    var position = 0, currentIndex = 0, slideIndex = 0;
    btnL.bind('click', function(e){
        e.preventDefault();
        if (slideIndex === 0) {
            return;
        }
        slideIndex--;
        position = -slideIndex * slideW;
        itembox.animate({
            'left': position
        });
    });
    btnR.bind('click', function(e){
        var temp;
        e.preventDefault();
        if (slideIndex == (temp = slideCount > 1 ? Math.ceil(count / slideCount - 1) : (count - 1))) {
            return;
        }
        slideIndex++;
        position = -slideIndex * slideW;
        itembox.animate({
            'left': position
        });
    });
}





function Comment(nid, options){

    return new Comment.prototype.init(nid, options);
}

Comment.prototype = {
    constructor: Comment,
    init: function(nid, options){
        this.nid = nid;
        
        this.reply_cid = $(options.reply_cid).val();
        this.commentText = $(options.commentTextareaId);
        this.participators = $(options.participatorsId);
        this.commentBtn = $(options.commentBtnId);
        this.commentsBox = $(options.commentsBoxId);
        /*
         this.commentText=$('#commentText');
         this.participators=$('#participators');
         this.commentBtn=$('#commentBtn');
         this.commentsBox=$('#commentsBox');
         */
        this.bindEvents();
        //add by yubin
        this.checkLimit();
    },
    checkLimit : function()
    {
        //this.commentText.css('height', 'auto');
        var max = max || 1000;// 最大限度字符，汉字按两个字符计算
        // 监听textarea的内容变化 
        var that = this;
        if(/msie (\d+\.\d)/i.test(navigator.userAgent) == true)
        {
            // 区分IE 
            this.commentText.get(0).onpropertychange = function(){that.txtLength(max)};
        }
        else
        {
            this.commentText.get(0).addEventListener("input", function(){that.txtLength(max)}, false); 
        }
        this.txtLength(max);//加载时先初始化 
    },
    txtLength : function(max)
    {
         // 内容变化时的处理 
        var text = this.commentText.val();
        var count = this.getTextLength(text);
        if(count > max)
        {
            // 文字超出截断 
            for(var i=0; i<text.length; i++)
            {
                if(this.getTextLength(text.substr(0, i)) >= max)
                {
                    $.alert('评论最多只能输入500个汉字');
                    this.commentText.val(text.substr(0, i));
                    break;
                };
            }
        }
        else
        {
            
        };
    },
    getTextLength : function(str)
    {
        // 获取字符串的长度 一个汉字为2个字符
        return str.replace(/[^\x00-\xff]/g,"xx").length;
    },
    checkLogin: function(){
    
        var ckcommentText = this.commentText.val();
        if( ckcommentText=='' ) {
        	$.alert('请填写评论！');
        	return false;
        }
        return true;//暂不检查是否登录
        /*
        var ckValue = cookie.get('voguebbsuid');
        if (!!ckValue && ckValue !== 'deleted') {
            return true;
        }
        else {
            $.alert('哥让你先登录 !!!');
            return false;
        }
        */
    },
    updateParticipators: function(count){
        this.participators.html(count);
    },
    updateCommentList: function(commentText){
        this.commentsBox.prepend(commentText);
    },
    bindEvents: function(){
    
        var self = this;
        this.commentBtn.bind('click', function(e){
            e.preventDefault();
            
            if (!self.checkLogin()) {
                return;
            };
            self.commentText.focus();
            var commentText = self.commentText.val();
            if(!commentText)
            {
                $.alert('评论内容不能为空');return;
            }
            $.alert('评论发布中...');
            $.ajax('http://www.vogue.com.cn/front_ajax/addcommentnew', {
                type: 'post',
                dataType: 'json',
                data: 'nid=' + self.nid + '&replycid='+self.reply_cid+'&comment=' + commentText,
                success: function(data, textStatus, jqXHR){
                    if(data.credit)
                    {
                        var arr = data.credit.split('::');
                        $_CNC_COMMON.creditShow(arr[0], arr[1]);
                    }
                    else
                    {
                        $.alert('发布成功');
                    }
                    self.commentText.val('');
                    self.commentText.blur();
                    self.updateParticipators(data.count);
                    self.updateCommentList(data.commentText);
                },
                error: function(jqXHR, textStatus, errorTrown){
                    $.alert('ajax error:' + textStatus);
                }
            });
        });
    }
}

Comment.prototype.init.prototype = Comment.prototype;


_vogue.save=function(){
var box=$('#shareFloatBox');
$('#loginPortId_03').bind('click',function(e){
	if(!!cookie.get('voguebbsuid')&&cookie.get('voguebbsuid')!=='deleted'){
		var self=$(this),pos=self.offset();
		alert(1);
		box.css({
			top:pos.top,
			left:pos.left+self.width()+5
		});
		box.show();
	}
});

$('#closeShareBox').bind('click',function(e){
    e.preventDefault();
	box.hide();
});

$('#shareBtn').bind('click',function(e){
	e.preventDefault();
	// your code here
});

}